package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;
import org.assertj.core.util.Strings;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 毛文杰
 *防疫中心-免疫记录表
 */
@Data
public class ImmunizationRecord implements Serializable {

    private static final ObjectMapper objectMapper= JsonMapper.nonEmptyMapper().getMapper();

    private static final long serialVersionUID = 9154405618317734486L;
    /**
     *
     */
    private Long id;
    /**
     *单据编号'
     */
    private String number;
    /**
     *状态，1：正常，-1：删除',
     */
    private Integer status;
    /**
     *'养殖场'
     */
    private Long breedFactoryId;
    /**
     *'养殖场名称'
     */
    private String breedFactoryName;
    /**
     * 操作人员id
     */
    private Long userId;

    /**
     * 操作人员name
     */
    private String userName;

    /**
     *'养殖品种：牛、马、猪',
     */
    private Integer breedType;

    private Integer breedSecondType;
    /**
     *'单位',
     */
    private Integer breedUnit;

    /**
     * 栏舍号id
     */
    private Long stableId;

    /**
     *'栏舍号',
     */
    private String stableNumber;


    /**
     * 仔存栏
     */
    private Integer yongStock;

    /**
     * 肉存栏
     */
    private Integer meatStock;

    /**
     * 公存栏
     */
    private Integer maleStock;

    /**
     * 母存栏
     */
    private Integer femaleStock;

    /**
     * 存栏
     */
    private Integer stock;

    /**
     * 圈舍面积
     */
    private Float acreage;

    /**
     * 耳标号
     */
    @JsonIgnore
    private String earNumberJson;

    /**
     * 不存数据库
     */
    private List<String> earNumbers;

    /**
     * 1：正常，-1：删除'
     */
    private Integer deleteFlag;
    /**
     *
     */
    private Date createdAt;
    /**
     *
     */
    private Date updatedAt;



    public void setEarNumbers(List<String> earNumbers){
        this.earNumbers = earNumbers;
        if(earNumbers == null || earNumbers.isEmpty()){
            this.earNumberJson = null;
        }else {
            try {
                this.earNumberJson =
                        objectMapper.writeValueAsString(earNumbers);
            } catch (JsonProcessingException e) {
                //this is fuck exception
            }
        }
    }


    public void setEarNumberJson(String earNumberJson){
        this.earNumberJson = earNumberJson;

        if(Strings.isNullOrEmpty(earNumberJson)){
            this.earNumbers = new ArrayList<>();
        }else {
            try {
                this.earNumbers =
                        objectMapper.readValue(earNumberJson, new TypeReference<List<String>>() {});
            } catch (IOException e) {
                //this is fuck exception
            }
        }
    }

}

